<?php
function array_length($arr){
    $length = 0;
    foreach($arr as $key =>$value){
        $length++;
    }
    return $length;
}
function array_impode($arr,$needle){
    $length = array_length($arr);
    $str = $needle;
    $i = 0;
    while($i <$length){
        $inst = $arr[$i];
        //$str .=$interests[$i];
        $str = $str.$inst.$needle;
        $i++;
    }
    return $str;
    

}

$haserror = false;
$username =$_POST["username"];
if(!$username) 
{
echo "用户名必须填写<br/>";
$haserror = true;
}else{
    $len = strlen($username);
    if($len<7){
        echo "用户名至少要有七个字符<br/>";
        $haserror = true;
    }
    else if($len >10){

        echo "用户名至多十个字符<br/>";
        $haserror = true;
    }else if(!preg_match('/^[a-zA-Z0-9]*$/',$username)){
        echo "格式不符合要求";
        $haserror = true;
    }
}

$pswd =trim( $_POST["password"]);
if(!$pswd)
{ 
echo "密码必须填写<br/>";
$haserror = true;
}else{
    $len = strlen($pswd);
    if($len<7){
        echo "密码至少要有七个字符<br/>";
        $haserror = true;
    }
    else if($len >10){

        echo "密码至多十个字符<br/>";
        $haserror = true;
    }
}
$age =trim( $_POST["age"]);
if($age){
    $ageNum = intval($age);
    if(!$ageNum || $ageNum<=0 || $ageNum>200){
        echo "请输入正确的年龄<br/>";
        $haserror = true;
    }
}

$interests = $_POST["interests"];


$length = array_length($interests);


$interestr = array_impode($interests,"|");
$province = $_POST["province"];
$intro = $_POST["intro"];

$gender = $_POST["gender"];
if($gender == 'male') $gender = 1;
else if($gender == 'female') $gender = 0;
else $gender = -1; 

$age = $_POST["age"];


if(!$haserror){
    $sql = "INSERT INTO  `looding` (`username`,`password`,`gender`,`interests`,`province`,
    `intro`,`age`) VALUES ('$username','$pswd','$gender','$interestr','$province',
    '$intro','$age') " ;
  

    $sqli = new mysqli("localhost","root","root","php");
   
    $sqli ->query($sql);
    if($sqli->error) echo $sqli->error;
    Header("location: login.html");
}



echo "<a href = 'base_html_form.html'>返回</a>";
